<?php
/**
 * Description: 找回密码
 *
 * @author      Eden
 * @datetime    2016/12/20 19:53
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Findpw_Model extends API_Model
{
    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 主程序
     * @param array $params
     */
    public function run($params = array())
    {
        $send['state'] = false;
        do {

            if (empty($params['phone']) || empty($params['newPassword'])) {
                $send['error'] = '参数错误';
                break;
            }

            if ($this->input->method(true) !== 'POST') {
                $send['error'] = '非法请求';
                break;
            }

            // 校验老密码
            $db = clone($this->db);
            $q = $db->get_where('user', array('phone' => $params['phone']));
            $row = $q->row_array();
            if (empty($row)) {
                $send['error'] = '用户不存在';
                break;
            }

            if (strlen($params['newPassword']) > 18 || strlen($params['newPassword']) < 6) {
                $send['error'] = '密码长度不符';
                break;
            }

            $passwords = encryptPassword($params['newPassword']);
            $userData['password'] = $passwords['password'];
            $userData['salt'] = $passwords['salt'];

            $this->db->where('phone', $params['phone']);
            $this->db->update('user', $userData);

            if ($this->db->affected_rows()) {
                $send['state'] = true;
                $send['data'] = array(
                    'msg' => '修改成功'
                );
            } else {
                $send['error'] = '修改失败';
            }

        } while(false);
        $this->output->send($send);
    }
}
